<?php

!defined('IN_NOVA') && exit('Access Denied!');

class Category {

	public function start() {
		global $request;
		$id = $request->id;
		$action = $request->get( 'p' );

		switch( $action ) {
			case 'addsort':
				$this->sort_add_data();
				break;
			case 'editsort':
				$this->sort_edit_data();
				break;
			case 'delsort':
				$this->sort_del_data();
				break;
			case "datacheckexe":
				$this->data_check_exe();
				break;
			default:
				$this->show( $action );
		}
	}

	function sort_add_data() {
		global $db,$request,$cache;
		$name = trim( $request->get( 'name','P' ) );
		$alias = trim( $request->get( 'alias','P' ) );
		$parent = $request->get( 'parent','P','int' );
		$description = trim( $request->get( 'description','P' ) );
		$num = $request->get( 'num','P','int' );
		if( $name == '' || $alias == '' || $description == '' || $parent === '' ) {
			echo '<p class="error">错误：您的输入不完整！</p>';
			return;
		}
		// 对 $alias 进行检查
		if( $db->result( "SELECT COUNT(cid) FROM `" . DB_PREFIX . "category` WHERE alias='$alias'" ) ) {
			echo '<p class="error">错误：分类别名已经存在！</p>';
			return;
		}
		/*$sort_folder = APP_ROOT . 'article/' . $alias . '/';
		if( !$this->writeable( $sort_folder ) ) {
			echo '<p class="error">错误：创建文件夹失败！</p>';
			return;
		}*/
		$db->query( "INSERT INTO `" . DB_PREFIX . "category` (`pid`,`num`,`name`,`alias`,`description`,`articlenum`) VALUES ($parent,$num,'$name','$alias','$description',0)" );
		$cache->refresh( 'category', TRUE );
		echo '<p class="warning">添加分类成功</p>';
	}

	function writeable($var) {
		$result = false;
		if ( !is_dir($var) ) {
			@mkdir($var, 0777);
		}
		if ( is_dir($var) ) {
			$var .= 'temp.txt';
			if ( ($fp=@fopen($var, 'wb')) && (@fwrite($fp, 'XKLog')) ) {
				@fclose($fp);
				@unlink($var);
				$result = true;
			}
		}
		return $result;
	}

	function sort_edit_data() {
		global $db,$request,$cache;
		$id = $request->get( 'id','P','int' );
		$name = $request->get( 'name','P' );
		$alias = $request->get( 'alias','P' );
		$parent = $request->get( 'parent','P','int' );
		$description = $request->get( 'description','P' );
		$num = $request->get( 'num','P','int' );
		if( $name == '' || $alias == '' || $description == '' || $parent === '' ) {
			echo '<p class="error">错误：您的输入不完整！</p>';
			return;
		}
		if( $db->result( "SELECT COUNT(cid) FROM `" . DB_PREFIX . "category` WHERE alias='$alias' AND cid<>$id" ) ) {
			echo '<p class="error">错误：分类别名重复！</p>';
			return;
		}
		/*$sort_folder = APP_ROOT . 'article/' . $alias . '/';
		$old_alias = $db->result( "SELECT alias FROM `" . DB_PREFIX . "category` WHERE cid=$id" );
		$path = APP_ROOT . 'article/';
		if ( !is_dir( $path . $old_alias ) ) {
			@mkdir( $path . $old_alias, 0777 );
		}
		@rename( $path . $old_alias, $path . $alias );
		if( !$this->writeable( $path . $alias ) ) {
			echo '<p class="error">错误：创建文件夹失败！</p>';
			return;
		}*/
		$db->query( "UPDATE `" . DB_PREFIX . "category` SET pid='$parent',name='$name',alias='$alias',description='$description',num='$num' WHERE cid=$id" );
		$cache->refresh( 'category', TRUE );
		echo '<p class="warning">修改分类成功</p>';
	}

	function sort_del_data() {
		global $db,$request,$cache;
		$id = $request->get( 'id', 'G', 'int');
		$article_num = $db->result( "SELECT COUNT(id) FROM `" . DB_PREFIX . "article` WHERE isdel=0 And category=$id" );
		if( $article_num != 0 ) {
			echo '<p class="error">这个分类不是空的，不能删除</p>';
		}else{
			$db->query( "DELETE FROM `" . DB_PREFIX . "category` WHERE cid=$id" );
			$cache->refresh( 'category', TRUE );
			echo '<p class="warning">删除分类成功</p>';
		}
	}

	function data_check_exe() {
		global $cache,$db;
		If( count( $cache->category ) == 0 ) {
			$this->show( 'datacheck', '尚无文章分类', 'error' );
			return;
		}else{
			foreach( $cache->category as $category ) {
				$cid = $category['cid'];
				$article_num = $db->result( "SELECT COUNT(id) FROM `" . DB_PREFIX . "article` WHERE isdel=0 And category=$cid" );
				if( $category['articlenum'] != $article_num ) {
					$db->query( "UPDATE `" . DB_PREFIX . "category` SET articlenum=$article_num WHERE cid=$cid" );
				}
			}
		}
		$cache->refresh( 'category', TRUE );
		echo '<p class="warning">分类数据已经重新统计</p>';
	}

	private function show( $action, $message = '', $type = '' ) {
		global $cache;
		if( $message != '' ) {
			$message = '<p class="' . $type . '">' . $message . '</p>';
		}
		@header("content-type: text/html; charset=utf-8");
?>
<div class="admin_panel">
	<?php
		echo $message;
		switch( $action ) {
		case "sort":
			$this->sort_list();
			break;
		case "add":
			$this->sort_add();
			break;
		case "edit":
			$this->sort_edit();
			break;
		case "datacheck":
			$this->data_check();
			break;
		default:
			$this->main();
		}
	?>
	<script>
		$(document).ready(function(){
			$('#ajax_form').ajaxForm(function(data){
				showMessage(data);
				$("#admin_loading").css('visibility','hidden');
			});
		});
	</script>
</div>
<?php
	}

	private function sort_list() {
		global $db;
		$sql = 'SELECT * FROM `' . DB_PREFIX . 'category` ORDER BY num ASC,cid DESC';
		$sort_array = $db->fetch_all( $sql );
?>
<div class="admin_title"><?php echo L('_CATEGORY_MANAGEMENT_'); ?></div>
<div class="admin_content">
	<div style="padding:5px;margin:2px;">
		<table style="table-layout: fixed;word-wrap: break-word;" width="100%" border="0" cellpadding="0" cellspacing="1" bgcolor="#dddddd">
			<tr align="center" height="22" bgcolor="#ffffff">
				<td width="3%"></td>
				<td width="4%"><?php echo L('_NUM_'); ?></td>
				<td width="17%"><?php echo L('_NAME_'); ?></td>
				<td width="6%">上级</td>
				<td width="14%"><?php echo L('_ALIAS_'); ?></td>
				<td width="6%"><?php echo L('_SORT_'); ?></td>
				<td width="33%"><?php echo L('_DESCRIPTION_'); ?></td>
				<td width="6%"><?php echo L('_NUMBER_'); ?></td>
				<td width="11%"><?php echo L('_OPERATION_'); ?></td>
			</tr>
			<?php
			foreach( $sort_array as $value ) {
			?>
			<tr align="center" height="20" bgcolor="#ffffff">
				<td ></td>
				<td><?php echo $value['cid'] ?></td>
				<td><?php echo $value['name'] ?></td>
				<td><?php echo $value['pid'] ?></td>
				<td><?php echo $value['alias'] ?></td>
				<td><?php echo $value['num'] ?></td>
				<td><?php echo $value['description'] ?></td>
				<td><?php echo $value['articlenum'] ?></td>
				<td>
					<span class="Control">
					<a href="#" onclick="ajax_load('<?php echo ADMIN_PATH ?>','category','edit',null,'<?php echo $value['cid'] ?>');return false;"><?php echo L('_EDIT_'); ?></a>&nbsp; &nbsp;
					<a href="#" onclick="if(confirm('<?php echo L('_DELETE_CONFIRM_1_'); ?>'))ajax_get('<?php echo ADMIN_PATH ?>','category','delsort',null,'<?php echo $value['cid'] ?>');return false;"><?php echo L('_DELETE_'); ?></a>
					</span>
				</td>
			</tr>
			<?php
			}
			?>
		</table>
	</div>
	<div class="main_button"></div>
</div>
<?php
	}

	private function sort_add() {
?>
<div class="admin_title"><?php echo L('_CATEGORY_ADD_'); ?></div>
<div class="admin_content">
	<form action="index.php?m=category&p=addsort" method="post" id="ajax_form">     
	<table border="0" cellpadding="2" cellspacing="1">
		<tr>
			<td width="180"><div align="right"><?php echo L('_NAME_'); ?>&nbsp;&nbsp;</div></td>
			<td align="left"><input name="name" type="text" size="30" maxlength="50" class="main_text" /></td>
		</tr>
		<tr>
			<td width="180"><div align="right"><?php echo L('_ALIAS_'); ?>&nbsp;&nbsp;</div></td>
			<td align="left"><input name="alias" type="text" size="30" maxlength="50" class="main_text" /></td>
		</tr>
		<tr>
			<td width="180"><div align="right"><?php echo L('_DESCRIPTION_'); ?>&nbsp;&nbsp;</div></td>
			<td align="left"><input name="description" type="text" size="30" maxlength="100" class="main_text" /></td>
		</tr>
		<tr>
			<td width="180"><div align="right">上级&nbsp;&nbsp;</div></td>
			<td align="left"><select name="parent"><option value="0">|</option><?php echo $this->get_category(); ?></select></td>
		</tr>
		<tr>
			<td width="180"><div align="right"><?php echo L('_SORT_'); ?>&nbsp;&nbsp;<div class="main_tips"><?php echo L('_SORT_TIPS_'); ?>&nbsp;&nbsp;</div></div></td>
			<td align="left"><input name="num" type="text" size="30" maxlength="10" class="main_text" /></td>
		</tr>
		<tr>
			<td width="180"><div align="right"></div></td>
			<td align="left"><input type="submit" onclick="$('#admin_loading').css('visibility','visible');" class="main_button" value=" <?php echo L('_ADD_'); ?> " /></td>
		</tr>
	</table>
	</form>
	<div class="main_button"></div>
</div>
<?php
	}

	private function get_category( $current = 0, $pid = 0, $deep = 0 ) {
		global $cache;
		$output = '';
		foreach ( $cache->category as $row ) {
			if( $row['pid'] != $pid ) continue;
			if( $current == $row['cid'] ) {
				$output .= '<option value="' . $row['cid'] . '" selected="selected">';
			} else {
				$output .= '<option value="' . $row['cid'] . '">';
			}
			for( $i = 0 ; $i < $deep ; $i ++ ) {
				$output .= '&nbsp;&nbsp;';
			}
			$output .= '|- →' . $row['name'] . '</option>';
			$output .= $this->get_category( $current, $row['cid'], $deep + 1 );
		}
		return $output;
	}

	private function sort_edit() {
		global $db,$request;
		$id = $request->get( 'id','G','int' );
		$sql = "SELECT * FROM `" . DB_PREFIX . "category` WHERE cid=$id";
		$sort_array = $db->fetch_one_array( $sql );
?>
<div class="admin_title">修改分类</div>
<div class="admin_content">
	<form action="index.php?m=category&p=editsort&id=<?php echo $id ?>" method="post"  id="ajax_form">
	<input name="id" type="hidden" value="<?php echo $id ?>" />
	<table border="0" cellpadding="2" cellspacing="1">
		<tr>
			<td width="180"><div align="right"><?php echo L('_NAME_'); ?>&nbsp;&nbsp;</div></td>
			<td align="left"><input name="name" type="text" size="30" maxlength="50" class="main_text" value="<?php echo $sort_array['name'] ?>" /></td>
		</tr>
		<tr>
			<td width="180"><div align="right"><?php echo L('_ALIAS_'); ?>&nbsp;&nbsp;</div></td>
			<td align="left"><input name="alias" type="text" size="30" maxlength="50" class="main_text" value="<?php echo $sort_array['alias'] ?>" /></td>
		</tr>
		<tr>
			<td width="180"><div align="right"><?php echo L('_DESCRIPTION_'); ?>&nbsp;&nbsp;</div></td>
			<td align="left"><input name="description" type="text" size="30" maxlength="100" class="main_text" value="<?php echo $sort_array['description'] ?>" /></td>
		</tr>
		<tr>
			<td width="180"><div align="right">上级&nbsp;&nbsp;</div></td>
			<td align="left"><select name="parent"><option value="0">|</option><?php echo $this->get_category( $sort_array['pid'] ); ?></select></td>
		</tr>
		<tr>
			<td width="180"><div align="right"><?php echo L('_SORT_'); ?>&nbsp;&nbsp;<div class="main_tips">必须是大于0的数字&nbsp;&nbsp;</div></div></td>
			<td align="left"><input name="num" type="text" size="30" maxlength="10" class="main_text" value="<?php echo $sort_array['num'] ?>" /></td>
		</tr>
		<tr>
			<td width="180"><div align="right"></div></td>
			<td align="left"><input type="submit" onclick="$('#admin_loading').css('visibility','visible');" class="main_button" value=" <?php echo L('_EDIT_'); ?> " /></td>
		</tr>
	</table>
	</form>
	<div class="main_button"></div>
</div>
<?php
	}

	private function data_check() {
?>
<div class="admin_title"><?php echo L('_CATEGORY_MANAGEMENT_'); ?></div>
<div class="admin_content">
	<div style="padding:10px;">
		<a href="#" onclick="ajax_get('<?php echo ADMIN_PATH ?>','category','datacheckexe');return false;"><?php echo L('_CATEGORY_DATA_CHECK_'); ?></a>
	</div>
</div>
<?php
	}

	private function main() {
?>
<div class="admin_title"><?php echo L('_CATEGORY_MANAGEMENT_'); ?></div>
<div class="admin_content">
	<div style="padding:10px;">
		<?php echo L('_CATEGORY_MESSAGE_'); ?>
	</div>
</div>
<?php
	}
}
?>
